using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Text;

using Mighty.Lib.Document;

namespace Mighty.Lib.Controls
{
	/// <summary>
	/// Summary description for TabPage.
	/// </summary>
	public class DefaultDocumentTabPage : MightyTabPage
    {

        #region properties

        RichTextBox _textBox = null;

        private bool _isSavable = true;
        public override bool IsSavable
        {
            get
            {
                return _isSavable;
            }
        }

        protected IDocument _doc = null;
        public IDocument Document
        {
            get
            {
                return _doc;
            }
            set
            {
                _doc = value;
                Text = _doc.Name;
                GenerateGUICodeFromIDocument();
            }
        }

        protected bool _hasBeenSavedSinceLastChange = true;
        public bool HasBeenSavedSinceLastChange
        {
            get
            {
                return _hasBeenSavedSinceLastChange;
            }
            set
            {
                _hasBeenSavedSinceLastChange = value;
                if (!_hasBeenSavedSinceLastChange && this.Text[this.Text.Length - 1] != '*')
                {
                    this.Text = this.Text + " *";
                }
                if (_hasBeenSavedSinceLastChange)
                {
                    this.Text = this.Document.Name;
                }
            }
        }

        #endregion

        #region ctors
        public DefaultDocumentTabPage(IDocument doc)
        {
            Document = doc;
        }
        public DefaultDocumentTabPage()
        {
            InitializeComponent();
        }
        private System.ComponentModel.Container components = null;
        public void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
        #endregion

        #region event handlers

        public void DefaultTextChangedEventHandler(object source, EventArgs e)
        {
            HasBeenSavedSinceLastChange = false;
        }

        #endregion

        public void Sync()
        {
            _doc.Contents = new System.Collections.Generic.List<string>(((RichTextBox)this.Controls[0]).Lines);
        }



        public void GenerateGUICodeFromIDocument()
        {
            Random rand = new Random();
            RichTextBox textBox1 = new RichTextBox();
            textBox1.AcceptsTab = true;
            textBox1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            textBox1.Location = new System.Drawing.Point(2, 3);
            textBox1.Name = "EditorRichTextBox" + rand.Next(0, 9999999).ToString();

            // TODO -- smarten up text box sizing algorithm...
            textBox1.Size = new System.Drawing.Size(723, 369);


            textBox1.TabIndex = 0;
            textBox1.TabStop = false;
            textBox1.ScrollBars = RichTextBoxScrollBars.Both;
            textBox1.Multiline = true;
            textBox1.WordWrap = false;

            textBox1.TextChanged += new EventHandler(DefaultTextChangedEventHandler);

            _textBox = textBox1;
            StringBuilder builder = new StringBuilder();
            _doc.Contents.ForEach((string line) =>
            {
                builder.Append(line);
            });
            textBox1.Text = builder.ToString();



            
            this.SuspendLayout();

            this.Controls.Clear();
            this.Controls.Add(textBox1);
            
            this.Location = new System.Drawing.Point(4, 22);
            this.Name = "tabPage" + rand.Next(0, 9999999).ToString();
            this.Padding = new System.Windows.Forms.Padding(3);
            this.Size = new System.Drawing.Size(728, 375);
            this.TabIndex = 0;

            this.UseVisualStyleBackColor = true;

            this.Click += new EventHandler(DefaultDocumentTabPage_Click);
            

            this.ResumeLayout();

            
        }

        void DefaultDocumentTabPage_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Click!");
            
            //((RichTextBox)this.Controls[_textBox.Name]).Focus();
        }

        #region overrides

        public override bool LameFocusOverload()
        {
            Controls[_textBox.Name].Select();
            return Controls[_textBox.Name].Focus();
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        #endregion
    }
}
